package com.CommunityHub.kiana.controller;


import com.CommunityHub.kiana.po.HouseInfo;
import com.CommunityHub.kiana.po.ResidenceInfo;
import com.CommunityHub.kiana.po.User;
import com.CommunityHub.kiana.service.MemberService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@WebServlet("/AddOccupantServlet")
public class AddOccupantServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        HttpSession session = request.getSession(false);
        if (session == null || session.getAttribute("user") == null) {
            response.sendRedirect("login.jsp");
            return;
        }

        User user = (User) session.getAttribute("user");
        int memberId = user.getId();

        MemberService memberService = new MemberService();
        try {
            // 获取已购房产
            List<HouseInfo> purchasedHouses = memberService.getPurchasedHouses(memberId);
            request.setAttribute("purchasedHouses", purchasedHouses);

            request.getRequestDispatcher("addOccupant.jsp").forward(request, response);
        } catch (SQLException e) {
            request.setAttribute("error", "加载房产数据失败: " + e.getMessage());
            request.getRequestDispatcher("memberHome.jsp").forward(request, response);
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        HttpSession session = request.getSession(false);
        if (session == null || session.getAttribute("user") == null) {
            response.sendRedirect("login.jsp");
            return;
        }

        User user = (User) session.getAttribute("user");
        int memberId = user.getId();

        // 获取表单参数
        int houseId = Integer.parseInt(request.getParameter("houseId"));
        String occupantName = request.getParameter("occupantName");
        int age = Integer.parseInt(request.getParameter("age"));
        String gender = request.getParameter("gender");
        String relationship = request.getParameter("relationship");
        String idCard = request.getParameter("idCard");
        String residenceType = request.getParameter("residenceType");

        // 创建入住人对象
        ResidenceInfo occupant = new ResidenceInfo();
        occupant.setHouseId(houseId);
        occupant.setMemberId(memberId);
        occupant.setOccupantName(occupantName);
        occupant.setAge(age);
        occupant.setGender(gender);
        occupant.setRelationship(relationship);
        occupant.setIdCard(idCard);
        occupant.setResidenceType(residenceType);
        occupant.setStartTime(new Timestamp(new Date().getTime())); // 入住开始时间为当前时间

        MemberService memberService = new MemberService();
        try {
            if (memberService.addOccupant(occupant)) {
                // 重新加载完整会员信息
                MemberService.CompleteMemberInfo info =
                        memberService.getCompleteMemberInfo(memberId);
                session.setAttribute("memberInfo", info);

                response.sendRedirect("ViewOccupantsServlet?houseId=" + houseId);
            } else {
                request.setAttribute("error", "添加入住人失败（权限不足）");
                request.getRequestDispatcher("addOccupant.jsp").forward(request, response);
            }
        } catch (SQLException e) {
            request.setAttribute("error", "系统错误: " + e.getMessage());
            request.getRequestDispatcher("addOccupant.jsp").forward(request, response);
        }
    }
}
